package com.mz.ozy.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.CompoundButton;
import android.widget.RadioButton;

import androidx.annotation.Nullable;

/**
 * 底部边框变色
 */
public class CustomRadioButton extends RadioButton {
    private Canvas mCanvas;
    private Context mContext;
    private int viewHeight;
    private int viewWidth;
    private boolean is_line=false;
    public CustomRadioButton(Context context) {
        super(context);
        mContext = context;
        setWillNotDraw(false);
        setCheckedChange();
    }
    public CustomRadioButton(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        setWillNotDraw(false);
        setCheckedChange();
    }
    private void setCheckedChange(){
        setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    is_line=true;
                }else{
                    is_line=false;
                }
                postInvalidate();
            }
        });
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mCanvas = canvas;
        viewHeight = getHeight();
        viewWidth = getWidth();
        if(is_line){
            bottomLine();
        }
    }
    private void bottomLine() {
        if(mCanvas!=null){
            Paint mPaint = new Paint();
            mPaint.setAntiAlias(true);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setColor(Color.parseColor("#316ff6"));
            mPaint.setStrokeWidth(20);
            mCanvas.drawLine(0, viewHeight, viewWidth, viewHeight, mPaint);
        }
    }
    
}
